package com.example.Jd_javaWeb_maven0402.model;

import com.example.Jd_javaWeb_maven0402.bean.UserInfoBean;
import com.example.Jd_javaWeb_maven0402.dao.DbProvider;
import com.example.Jd_javaWeb_maven0402.uilts.MyUilts;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.util.List;
import java.util.Map;

public class UserModel {
    /**
     * 添加用户
     * @param userInfoBean 用户类型
     * @return 返回变量
     */
    public int addUserInfo(UserInfoBean userInfoBean){
        //默认返回值
        int i = 0;
        //数据保存的sql语句
        String strSql = "insert into userinfo(userName,userPsw,phone,userDate) values(?,?,?,?)";
        i = MyUilts.getNewInstance(DbProvider.class).execute(strSql,
                new Object[]{
                        userInfoBean.getUserName(),
                        userInfoBean.getUserPsw(),userInfoBean.getPhone(),null
                });
        return i;
    }
    /**
     * 验证用户是否登录成功
     *
     * @param userName 用户名
     * @param userPsw  密码
     * @return 验证结果
     */
    public boolean doLogin(String userName, String userPsw){
        //默认返回值
        boolean canLogin = false;
        //查询语句
        String strSql = "select * from userInfo where username = ? and userpsw = ?";
        //创建查询器
       // DbProvider dbProvider = new DbProvider();
        //判断查询是否成功
        //List<Map<String,Object>> list = dbProvider.requryListMap(strSql, userName,userPsw);
        List<Map<String,Object>> list = MyUilts.getNewInstance(DbProvider.class).query(strSql, new MapListHandler(), userName, userPsw);
        if (list != null && list.size() > 0){
            canLogin = true;
        }
        return canLogin;
    }


}
